package org.ipower.examsystem.service.impl;
import java.util.HashMap;
import org.ipower.examsystem.service.SystemMainModules;
import org.ipower.platform.cfg.ModuleDefine;
import org.ipower.platform.cfg.ModuleDefineCollection;
import org.ipower.platform.cfg.ModuleDefineFactory;
import org.ipower.platform.cfg.ModuleSystemDefine;
import org.ipower.platform.cfg.ModuleSystemDefineCollection;
import org.ipower.platform.service.TreeData;
import org.ipower.platform.service.TreeDataCollection;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;

/*
 * 系统主模块实现类。
 * */
public class SystemMainModulesImpl implements SystemMainModules {
    private static final long serialVersionUID = -5347423520234146448L;
    private String path,systemID;
    private TreeDataCollection children;
    private static HashMap<String,TreeDataCollection> cache = new HashMap<String,TreeDataCollection>();
    /**
     * 设置配置文件路径。
     * @param path 文件路径。
     * */
    @Override
    public void setPath(String path) {
        this.path = path;
    }
    /**
     * 设置系统ID。
     * @param systemID 系统ID。
     * */
    @Override
    public void setSystemID(String systemID) {
        if(systemID != null){
            this.systemID = systemID;
        }
    }
    /**
     * 获取所有子集合。
     * @return 子集合。
     * */
    @Override
    public TreeDataCollection getChildren() {
        return this.children;
    }
    /**
     * 构建主菜单数据。
     * */
    @Override
    public void BuildMainModules() throws Exception{
        this.children = cache.get(this.systemID);
        if(this.children == null){
            Resource resource = new ClassPathResource(this.path.trim(),ClassUtils.getDefaultClassLoader());
            ModuleSystemDefineCollection  systems =  ModuleDefineFactory.DeSerialize(resource.getInputStream());
            if(systems != null && systems.size() > 0){
                    ModuleSystemDefine sys;
                    if(this.systemID.isEmpty()){
                        sys = systems.item(0);
                        this.systemID = sys.getSystemID();
                    }else{
                        sys = systems.item(this.systemID);
                    }
                    if(sys != null){
                        this.children = this.buildModulesTreeData(sys.getModules());
                        if(this.children != null && this.children.size() > 0){
                                cache.put(this.systemID, this.children);
                        }
                    }
            }
        }
    }
    /**
     * 构建模块树结构数据。
     * */
    protected TreeDataCollection buildModulesTreeData(ModuleDefineCollection modules){
        TreeDataCollection collection = new TreeDataCollection();
        if(modules != null && modules.size() > 0){
            for(ModuleDefine m : modules){
                TreeData tree = new  TreeData();
                tree.setID(m.getModuleID());
                tree.setText(m.getModuleName());
                tree.setUri(m.getModuleUri());
                tree.setIndex(m.getOrderNo());
                tree.setExpanded(true);
                if(m.getModules().size() > 0){
                    tree.setLeaf(false);
                    tree.setChildren(this.buildModulesTreeData(m.getModules()));
                }else{
                    tree.setLeaf(true);
                }
                collection.add(tree);
            }
        }
        return collection;
    }
}